package com.han.hotelplat.controller.wallet;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.han.hotelplat.cache.redis.IRedisCache;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.ResponseWrapTo.CodeType;
import com.han.hotelplat.comm.utils.MD5;
import com.han.hotelplat.comm.utils.MsgHelp;
import com.han.hotelplat.comm.utils.NumberUtil;
import com.han.hotelplat.comm.utils.RequestUtil;
import com.han.hotelplat.manager.LogManager;
import com.han.hotelplat.model.TaskLogModel;
import com.han.hotelplat.pojo.employee.Employee;
import com.han.hotelplat.pojo.hotelmanager.Hotelmanager;
import com.han.hotelplat.pojo.log.Logger;
import com.han.hotelplat.pojo.systemuser.Systemuser;
import com.han.hotelplat.pojo.task.Task;
import com.han.hotelplat.pojo.wallet.Wallet;
import com.han.hotelplat.service.childtask.ChildTaskService;
import com.han.hotelplat.service.task.TaskService;
import com.han.hotelplat.service.wallet.WalletService;

@Controller
@RequestMapping("/wallet/*")
public class WalletController {
	@Autowired
	private ChildTaskService childTaskService;
	@Autowired
	private WalletService walletService;
	@Autowired
	private IRedisCache redisCache;
	@Autowired
	private TaskService taskService;
	@Autowired
	private LogManager logManager;
	
	
	
	@ResponseBody
	@RequestMapping(value = "payOrderTaskForHotel", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String payOrderTaskForHotel(String takid, String password){//对任务划账
		Hotelmanager hotelmanager = (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(hotelmanager == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		//Wallet wallet= walletService.getWallet(hotelmanager.getHotelid().longValue());//酒店钱包是用酒店ID作为用户名的
		/*String mypassword = wallet.getPaypassword();
		if(!MD5.toMD5(password).equals(mypassword)){
			return new ResponseWrapTo(-1, "支付密码错误", null).toJson();
		}*/
		Task t = taskService.getHotelTaskDteail(takid);
		if(t.getState().intValue() == Constant.TASK_STATE_STOP.intValue()){
			return  new ResponseWrapTo(-2, "任务已经停止不能继续提交考勤了", null).toJson();
		}
		Map<String, Object> re = childTaskService.paymentChildTaskToEm(takid, 
				hotelmanager.getHotelid(), hotelmanager.getUserid(),2, hotelmanager.getRealname());
		
		int code = (int) re.get("code");
		if(code == 0){
			String host = RequestUtil.getRequest().getRemoteHost();
			Logger logger = new Logger(new Date(), hotelmanager.getUserid(), host,hotelmanager.getRealname());
			String context = "确认任务已完成";
			TaskLogModel tlm = new TaskLogModel();
			tlm.setContext(context);
			tlm.setOptcode(Constant.logger.LOG_TASK_FINISH);
			tlm.setTaskid(takid);
			logManager.creatTaskLog(tlm, logger);
		}
		String message = (String) re.get("message");
		return  new ResponseWrapTo(code, message, null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "getWalletBalance", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getWalletBalance(Integer userType){
		String userid = null;
		if(userType.equals(Constant.USER_TYPE_EMPLOYEE) ){//帮工
			Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(em == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = em.getUserid();
		}
		if(userType.equals(Constant.USER_TYPE_HOTELMANAGER) ){//酒店管家
			Hotelmanager hm =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(hm == null){
				hm = new Hotelmanager();
				hm.setHotelid(2);//测试接口用
			}
			userid = hm.getHotelid().toString();//酒店钱包用户ID使用酒店ID
		}
		if(userType.equals(Constant.USER_TYPE_PLATFORM) ){//平台
			Systemuser sm = (Systemuser) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(sm == null){
				//return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = Constant.WALLET_ID.toString();//平台钱包是固定不变的只有一个
		}
		Wallet wallet = walletService.getWallet(userid);
		if(wallet == null){
			return new ResponseWrapTo(-1, "钱包还未被激活",null).toJson();
		}
		wallet.setPaypassword(null);
		return new ResponseWrapTo(CodeType.success, "数据获取成功",wallet).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "verifyPasssword", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String verifyPasssword(Integer userType, String oldpassword){
		String userid = null;
		if(userType.equals(Constant.USER_TYPE_EMPLOYEE) ){//帮工
			Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(em == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = em.getUserid();
		}
		if(userType.equals(Constant.USER_TYPE_HOTELMANAGER) ){//酒店管家
			Hotelmanager hm =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(hm == null){
				hm = new Hotelmanager();
				hm.setHotelid(2);//测试接口用
			}
			userid = hm.getHotelid().toString();//酒店钱包用户ID使用酒店ID
		}
		if(userType.equals(Constant.USER_TYPE_PLATFORM) ){//平台
			Systemuser sm = (Systemuser) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(sm == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = Constant.WALLET_ID.toString();//平台钱包是固定不变的只有一个
		}
		Wallet wallet = walletService.getWallet(userid);
		if(wallet.getState().intValue() == Constant.WALLET_STATE_NOTACTIVE.intValue()){
			return new ResponseWrapTo(CodeType.fail, "钱包未设置过密码", null).toJson();
		}
		if(wallet == null || !wallet.getPaypassword().equals(MD5.toMD5(oldpassword))){//密码错误不能修改
			RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_NOPASS);
			//返回码固定-3
			return new ResponseWrapTo(-3,"旧密码错误",null).toJson();
		}
		RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_PASS);
		RequestUtil.setSessionAttribute(Constant.USERWALLET, wallet);
		return new ResponseWrapTo(CodeType.success, "验证成功", null).toJson();
		
	}
	
	@ResponseBody
	@RequestMapping(value = "modifyWalletPasssword", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String modifyWalletPasssword(Integer userType, String password){
		Integer result = (Integer) RequestUtil.getSessionAttribute("verifyVCode");
		//已经验证过
		if(result.intValue() == Constant.VERIFYVCODE_PASS.intValue()){
			Wallet wallet = (Wallet) RequestUtil.getSessionAttribute(Constant.USERWALLET);
			if(wallet == null){
				return new ResponseWrapTo(-3, "超时请重新输入旧密码", null).toJson();
			}
			Map<String, Object> re = walletService.setWalletPayPassword(wallet.getUserid(), password);
			int code = (int) re.get("code");
			String message = (String) re.get("message");
			if(code == 0){
				return new ResponseWrapTo(CodeType.success, "密码修改成功", null).toJson();
			}else{
				return new ResponseWrapTo(CodeType.fail, message, null).toJson();
			}
		}
		return new ResponseWrapTo(CodeType.fail, "还未验证旧密码或验证失败，请重新验证旧密码", null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "getWalletState", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getWalletState(Integer userType){
		String userid = null;
		if(userType.equals(Constant.USER_TYPE_EMPLOYEE) ){//帮工
			Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(em == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = em.getUserid();
		}
		if(userType.equals(Constant.USER_TYPE_HOTELMANAGER) ){//酒店管家
			Hotelmanager hm =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(hm == null){
				hm = new Hotelmanager();
				hm.setHotelid(2);//测试接口用
			}
			userid = hm.getHotelid().toString();//酒店钱包用户ID使用酒店ID
		}
		if(userType.equals(Constant.USER_TYPE_PLATFORM) ){//平台
			Systemuser sm = (Systemuser) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(sm == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = Constant.WALLET_ID.toString();//平台钱包是固定不变的只有一个
		}
		Wallet wallet = walletService.getWallet(userid);
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",new JSONObject().put("state", wallet.getState())).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "payOrder", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String payOrder(String childtaskids, String password){//对子任务划账
		Hotelmanager hotelmanager = (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(hotelmanager == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		/*Wallet wallet= walletService.getWallet(hotelmanager.getHotelid().longValue());//酒店钱包是用酒店ID作为用户名的
		String mypassword = wallet.getPaypassword();
		if(!MD5.toMD5(password).equals(mypassword)){
			return new ResponseWrapTo(-1, "支付密码错误", null).toJson();
		}*/
		Map<String, Object> re = childTaskService.paymentChildTask(childtaskids, 
				hotelmanager.getHotelid(), hotelmanager.getUserid(), hotelmanager.getRealname());
		int code = (int) re.get("code");
		String message = (String) re.get("message");
		return  new ResponseWrapTo(code, message, null).toJson();
	}
	
	/**
	 * 
		 * @description	获取验证码
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getPayPasswordVC", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getPayPasswordVC(Integer userType){
		String userid = null;
		String phone = "";
		if(userType.equals(Constant.USER_TYPE_EMPLOYEE) ){//帮工
			Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(em == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = em.getUserid();
			phone = em.getTel();
		}
		if(userType.equals(Constant.USER_TYPE_HOTELMANAGER) ){//酒店管家
			Hotelmanager hm =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(hm == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			if(hm.getHotelid() == null || hm.getHotelid().intValue() == -1){
				return new ResponseWrapTo(CodeType.fail, "还未提交酒店资料不能进行设置", null).toJson();
			}
			userid = hm.getHotelid().toString();//酒店钱包用户ID使用酒店ID
			phone = hm.getTel();
		}
		/*if(userType.equals(Constant.USER_TYPE_PLATFORM) ){//平台
			Systemuser sm = (Systemuser) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(sm == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			userid = Constant.WALLET_ID.longValue();//平台钱包是固定不变的只有一个
		}*/
		Wallet wallet = walletService.getWallet(userid);
		
		//将用户钱包存进session
		RequestUtil.setSessionAttribute(Constant.USERWALLET, wallet);
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_PAYPASSWORD+phone, vcode);
		try {
			MsgHelp.sendCheckCode(phone, vcode+"",userType);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
		
	}

	/**
	 * 
		 * @description	验证码验证
		 * @author  han1088
		 * @param phone
		 * @param vcode
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "verifyVCode", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String verifyVCode(Integer userType,String vcode){
		String phone = "";
		if(userType.equals(Constant.USER_TYPE_EMPLOYEE) ){//帮工
			Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(em == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			phone = em.getTel();
		}
		if(userType.equals(Constant.USER_TYPE_HOTELMANAGER) ){//酒店管家
			Hotelmanager hm =  (Hotelmanager) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
			if(hm == null){
				return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
			}
			phone = hm.getTel();
		}
		String cacheVCode = redisCache.get(Constant.CACHE_PHONE_VCODE_PAYPASSWORD+phone);
		if(cacheVCode == null){
			//返回值固定-3
			return new ResponseWrapTo(-3, "验证码失效请重新获取", null).toJson();
		}
			//返回值固定-3
		if(!cacheVCode.equals(vcode)){
			RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_NOPASS);
			return new ResponseWrapTo(-3, "验证码错误", null).toJson();
		}
		RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_PASS);
		redisCache.delete(Constant.CACHE_PHONE_VCODE_PAYPASSWORD+phone);
		return new ResponseWrapTo(CodeType.success, "验证成功", null).toJson();
	}

	/**
	 * 
		 * @description	设置密码
		 * @author  han1088
		 * @param password
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "setPayPassword", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String setPayPassword(String password){
		Integer result = (Integer) RequestUtil.getSessionAttribute("verifyVCode");
		//已经验证过
		if(result.intValue() == Constant.VERIFYVCODE_PASS.intValue()){
			Wallet wallet = (Wallet) RequestUtil.getSessionAttribute(Constant.USERWALLET);
			if(wallet == null){
				return new ResponseWrapTo(-3, "超时请重新获取验证码", null).toJson();
			}
			Map<String, Object> re = walletService.setWalletPayPassword(wallet.getUserid(), password);
			int code = (int) re.get("code");
			String message = (String) re.get("message");
			if(code == 0){
				return new ResponseWrapTo(CodeType.success, "密码设置成功", null).toJson();
			}else{
				return new ResponseWrapTo(CodeType.fail, message, null).toJson();
			}
		}
		return new ResponseWrapTo(CodeType.fail, "验证码错误", null).toJson();
	}
}
